<%--
  Created by IntelliJ IDEA.
  User: acer
  Date: 2016/5/19
  Time: 10:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>页首</title>

    <script src="/ssm/js/common.js"></script>

    <script type='text/javascript' src='/ssm/webjars/jquery/1.11.1/jquery.min.js'></script>
    <script src="/ssm/js/jquery.cookie.js"></script>
    <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>

    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <%--<!-- 可选的 Bootstrap 主题文件（一般不用引入） -->--%>
    <%--<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">--%>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>

    <%--<link rel='stylesheet' href='/ssm/webjars/bootstrap/3.3.6/css/bootstrap.min.css'>--%>

    <%--<script type='text/javascript' src='/ssm/webjars/bootstrap/3.3.6/js/bootstrap.min.js'></script>--%>
</head>
<body>
<div class="container-fluid" id="header">
    <ul class="nav nav-tabs">
        <li><a href="/ssm/index.jsp">首页</a></li>
        <li><a href="/ssm/organization/query">查学校关系</a></li>
        <li><a href="/ssm/user/chart">用户统计</a></li>
        <li><a href="/ssm/product">文件上传</a></li>
        <form id="login_form" class="form-inline" action="/ssm/login" style="float: right;vertical-align:middle;">
            <div id="loginBlock">
                <div class="input-group">
                    <span class="input-group-addon">用户名：</span>
                    <input type="text" class="form-control" id="userName" name="userName">
                </div>
                <div class="input-group">
                    <span class="input-group-addon">密码：</span>
                    <input type="password" class="form-control" id="userPassword" name="userPassword">
                </div>
                <button id="loginButton" type="submit" class="btn btn-default">登陆</button>
                <button id="registerButton" type="button" class="btn btn-default"
                        onclick="window.location='/ssm/register.jsp'">注册
                </button>
            </div>
            <div id="welcomeBlock" style="display:none">
                <span id="welcome"></span>
                <a href="#" id="logoutButton">注销</a>
            </div>
        </form>

    </ul>

</div>
</body>
<script>
    $(function () {
        contextPath = "http://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/";

        if ($.cookie('userName')) {
            $("#loginBlock").hide();
            $("#welcome").html("欢迎您," + $.cookie('userName'));
            $("#welcomeBlock").show();
        } else {
            $.ajax(contextPath + "check", {
                type: "get",
                dataType: 'json',
                success: function (data) {
                    $("#loginBlock").hide();
                    $("#welcome").html("欢迎您," + data);
                    $("#welcomeBlock").show();
//                alert(document.cookie.toString());
                }
            });
        }

        $("#logoutButton").bind("click", function () {
            logout();
        });

//        $("#loginButton").bind("click", function () {
//            $.ajax("http://localhost:8080/ssm/login",
//                    {
//                        type: "post",
//                        data: "userName=" + $("#userName").val() + "&userPassword=" + $("#userPassword").val(),
//                        success: function (data) {
////                            alert(data);
//                            //alert($("#userName").val()+$("#userPassword").val());
//                            $("#header form").html("欢迎您," + data);
//                        }
//                    });
//        })
    });

    function logout() {
        $.ajax(contextPath + "logout", {
            type: "get",
            dataType: 'json',
            success: function (result) {
                if (result.success) {
                    $.removeCookie('userName', {path: '/ssm/'});
                    window.location = '/ssm/index.jsp';
                } else {
                    alert("退出失败");
                }
            }
        });
    }
</script>
</html>
